package com.egao.api.controller.nont;


import com.egao.common.core.Constants;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.utils.FileUploadUtil;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.orderB.entity.Bill;
import com.egao.orderB.service.BillFeeService;
import com.egao.orderB.service.BillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * https://francjc.com:4332/swagger-ui.html
 */
@RequestMapping("/api/nt/bill")
@RestController
@Api(value = "订单管理",tags = {"订单管理"})
public class ApiBillNtController extends BaseController {

    @Autowired
    private OperRecordService operRecordService;
    @Autowired
    private BillService billService;
    @Autowired
    private BillFeeService billFeeService;

    @GetMapping("/exportsFinance")
    @ApiOperation(value = "导出财务报表",notes = "" +
        "limit: 1000\n" +
        "page: 1\n" +
        "state: 应收费用已审核\n" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "billOfLading: 123\n" +
        "qrCode: financeList\n" +
        "carTeam: 车队\n" +
        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsFinance(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "finance.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        JsonResult jr = billService.list(page,bill,new User(1));
        if((long)jr.get("count")>0) {
            billService.exportsFinance(jr, bill);
            //http://localhost:9331/api/nt/bill/exportsFinance
            FileUploadUtil.download("finance.xlsx", response);
        }
    }

    @GetMapping("/exportsFinance2")
    @ApiOperation(value = "导出财务报表",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "dock: 港口\n" +
//        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsFinance2(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "finance.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<Bill> billList = billService.financeExportsList(bill);
        if(billList != null && billList.size() > 0){
            Bill bill2 = new Bill();
            bill2.setType("应收");
            bill2.setDate1(bill.getDate1());
            bill2.setDate2(bill.getDate2());
            bill2.setKeywords(bill.getCustomer());
            List<String> feeNamesys = billFeeService.feeNames(bill2, null, new User(1));
            bill2.setType("应付");
            List<String> feenames = new ArrayList<>();
            feenames.add("报关费");
            List<String> feeNamesyf = billFeeService.feeNames(bill2, feenames, new User(1));
            feeNamesyf.add("报关费");
            if(feeNamesys != null && feeNamesys.size() > 0
                    &&feeNamesyf != null && feeNamesyf.size() > 0
            ) {
                billService.exportsFinance2(billList,feeNamesys, feeNamesyf, bill);
                //http://localhost:9331/api/nt/bill/exportsFinance
                FileUploadUtil.download("finance.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        } else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }
    @GetMapping("/exportsFinance3")
    @ApiOperation(value = "导出财务报表 应收只要运费  应付只要抬头是温州甩手",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "dock: 港口\n" +
//        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsFinance3(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "finance.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<Bill> billList = billService.financeExportsList2(bill);
        if(billList != null && billList.size() > 0){
            Bill bill2 = new Bill();
            bill2.setType("应收");
            bill2.setDate1(bill.getDate1());
            bill2.setDate2(bill.getDate2());
            bill2.setKeywords(bill.getCustomer());
//            List<String> feeNamesys = billFeeService.feeNames(bill2, null, new User(1));
            List<String> feeNamesys = new ArrayList<>();
            feeNamesys.add("运费");
            bill2.setType("应付");
            List<String> feenames = new ArrayList<>();
            feenames.add("报关费");
            bill2.setOrganizationId("甩手温州");
            List<String> feeNamesyf = billFeeService.feeNames(bill2, feenames, new User(1));
            feeNamesyf.add("报关费");
            if(feeNamesys != null && feeNamesys.size() > 0
                    &&feeNamesyf != null && feeNamesyf.size() > 0
            ) {
                billService.exportsFinance2(billList,feeNamesys, feeNamesyf, bill);
                //http://localhost:9331/api/nt/bill/exportsFinance
                FileUploadUtil.download("finance.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        } else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }

    @GetMapping("/exportsFinanceClassify")
    @ApiOperation(value = "应收应付分类导出财务报表",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "dock: 港口\n" +
//        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsFinanceClassify(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "finance.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<Bill> billList = billService.financeExportsClassifyList(bill);
        if(billList != null && billList.size() > 0){
            billService.exportsFinanceClassify(billList, bill);
            //http://localhost:9331/api/nt/bill/exportsFinance
            FileUploadUtil.download("finance.xlsx", response);
        } else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }

    @GetMapping("/exportsCarTeam")
    @ApiOperation(value = "导出车队",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
//        "值为customer: 值为对应客户 表示导出客户应收账单\n" +
        "值为driver: 值为对应司机 表示导出司机应付账单\n" +
        "值为carNo: 值为对应车牌 表示导出司机应付账单\n" +
        "值为carTeam: 值为对应车队表示导出车队应付账单\n" +
        "type: 类型  应收/应付\n" +
        "",httpMethod = "GET")
    public void exportsCarTeam(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "customer.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<Bill> billList = billService.exportsList(bill);
        if(billList != null && billList.size() > 0) {
            List<String> feeNames = billFeeService.feeNames(bill,null,  new User(1));
            if(feeNames != null && feeNames.size() > 0) {
                billService.exportsCarTeam(billList,feeNames, bill);
                //http://localhost:9331/api/nt/bill/exportsFinance
                FileUploadUtil.download("customer.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        }else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }

    }

    @GetMapping("/exportsCustomer")
    @ApiOperation(value = "导出客户",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "值为customer: 值为对应客户 表示导出客户应收账单\n" +
//        "值为driver: 值为对应司机 表示导出司机应付账单\n" +
//        "值为carNo: 值为对应车牌 表示导出司机应付账单\n" +
//        "值为carTeam: 值为对应车队表示导出车队应付账单\n" +
        "type: 应收,应付\n" +
        "",httpMethod = "GET")
    public void exportsCustomer(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "customer.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<Bill> billList = billService.exportsList(bill);
        if(billList != null && billList.size() > 0) {
            List<String> feeNames = billFeeService.feeNames(bill,null,  new User(1));
            if(feeNames != null && feeNames.size() > 0) {
                billService.exportsCustomer(billList,feeNames, bill);
                //http://localhost:9331/api/nt/bill/exportsFinance
                FileUploadUtil.download("customer.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        }

    }

    //做的不对  没有用
//    @GetMapping("/exportsCustomerBillAndApply")
//    @ApiOperation(value = "导出客户拖车和报关的账单",notes = "" +
//        "date1: 2021-01-29\n" +
//        "date2: 2021-01-30\n" +
//        "值为customer: 值为对应客户 表示导出客户应收账单\n" +
////        "值为driver: 值为对应司机 表示导出司机应付账单\n" +
////        "值为carNo: 值为对应车牌 表示导出司机应付账单\n" +
////        "值为carTeam: 值为对应车队表示导出车队应付账单\n" +
//        "type: 应收,应付\n" +
//        "",httpMethod = "GET")
    public void exportsCustomerBillAndApply(PageResult2<T> page, Bill bill, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "customer.xlsx");
        if(file.exists()) {
            file.delete();
        }
        bill.setDate1(bill.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(bill.getDate2())){
            bill.setDate2(bill.getDate2().substring(0,10)+" 23:59:59");
        }
        bill.setApplyId("billAndApply");
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<Bill> billList = billService.exportsList(bill);
        if(billList != null && billList.size() > 0) {
            List<String> feeNames = billFeeService.feeNamesBillAndApply(bill,null,  new User(1));
            if(feeNames != null && feeNames.size() > 0) {
                billService.exportsCustomer(billList,feeNames, bill);
                //http://localhost:9331/api/nt/bill/exportsFinance
                FileUploadUtil.download("customer.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        }

    }

}
